"""
封装requests发送get,post请求
1. 创建session，使用session发送get,post请求
2. 打印日志，方便定位
3. 异常处理
"""
import requests


class BaseRequest:
    def __init__(self):
        self.session = requests.session()

    def get(self, url, **kwargs):
        try:
            print("发送get请求，url:", url, "参数：", kwargs)
            r = self.session.get(url, **kwargs)
            print("发送get请求成功，响应:", r.text)
            return r
        except Exception as e:
            print("发送get请求异常，url", url, "参数：", kwargs, "异常信息:", e)

    def post(self, url, **kwargs):
        try:
            print("发送post请求，url:", url, "参数：", kwargs)
            r = self.session.post(url, **kwargs)
            print("发送post请求成功，响应:", r.text)
            return r
        except Exception as e:
            print("发送post请求异常，url", url, "参数：", kwargs, "异常信息:", e)


if __name__ == '__main__':
    test_request = BaseRequest()
    cs = {
        "username": "root"
    }
    test_request.get("http://httpbin.org/get",  params=cs)
    test_request.post("http://httpbin.org/post", data=cs)
